//
//  MBSpaceUserHeaderView.m
//

//
//  Created by admin on 16/9/14.
//  Copyright © 2016年 pegnk. All rights reserved.
//

#import "MBSpaceUserHeaderView.h"

@interface MBSpaceUserHeaderView () {
    UIImageView                 *bgImgView;
    UIImageView                 *_userImgView;
    UIButton                    *useImgbt;
    UILabel                     *_nickNameLb;
    UILabel                     *_userIDLb;
    UILabel                     *_userDetailLb;
    UIButton                    *useDetailbt;
    UIView                      *bottomView;
    UILabel                     *_gzLb;
    UILabel                     *_fanceLb;
    UILabel                     *_qzxLb;
}

@end
@implementation MBSpaceUserHeaderView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self onCreateUI];
    }
    return self;
}

- (void)onCreateUI
{
    //背景
    bgImgView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, self.height)];
    bgImgView.backgroundColor = [UIColor clearColor];
    bgImgView.image = [UIImage imageNamed:@"bg_zy"];
    [self addSubview:bgImgView];
    
    //头像
    _userImgView = [[UIImageView alloc]initWithFrame:CGRectMake((SCREEN_WIDTH-140*PERCEN)/2., 168*PERCEN, 140*PERCEN, 140*PERCEN)];
    _userImgView.backgroundColor = [UIColor clearColor];
    [self addSubview:_userImgView];
    
    useImgbt = [UIButton buttonWithType:UIButtonTypeCustom];
    useImgbt.frame = _userImgView.frame;
    useImgbt.backgroundColor = [UIColor clearColor];
    [useImgbt addTarget:self action:@selector(onSelectPicImgAction) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:useImgbt];
    
    //昵称
    _nickNameLb = [[UILabel alloc]initWithFrame:CGRectMake(0, _userImgView.bottom+10*PERCEN, SCREEN_WIDTH, 20)];
    _nickNameLb.backgroundColor = [UIColor clearColor];
    _nickNameLb.textAlignment = NSTextAlignmentCenter;
    _nickNameLb.textColor = UIColorFromHex(0xffffff);
    _nickNameLb.font = [UIFont systemFontOfSize:14.f];
    [self addSubview:_nickNameLb];
    
    //用户UD
    _userIDLb = [[UILabel alloc]initWithFrame:CGRectMake(0, _nickNameLb.bottom-2, SCREEN_WIDTH, 20)];
    _userIDLb.backgroundColor = [UIColor clearColor];
    _userIDLb.textAlignment = NSTextAlignmentCenter;
    _userIDLb.textColor = UIColorFromHex(0xffffff);
    _userIDLb.font = [UIFont systemFontOfSize:12.f];
    [self addSubview:_userIDLb];
    
    //用户描述
    _userDetailLb = [[UILabel alloc]initWithFrame:CGRectMake(0, (WIDTH_5S ? _userIDLb.bottom-5*PERCEN : _userIDLb.bottom+3*PERCEN), SCREEN_WIDTH, 20)];
    _userDetailLb.backgroundColor = [UIColor clearColor];
    _userDetailLb.textAlignment = NSTextAlignmentCenter;
    _userDetailLb.textColor = UIColorFromHex(0xffffff);
    _userDetailLb.font = [UIFont systemFontOfSize:12.f];
    [self addSubview:_userDetailLb];
    
    useDetailbt = [UIButton buttonWithType:UIButtonTypeCustom];
    useDetailbt.frame = _userDetailLb.frame;
    useDetailbt.backgroundColor = [UIColor clearColor];
    [useDetailbt addTarget:self action:@selector(onSelectUserDescribeAction) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:useDetailbt];
    
    //**************关注、粉丝、亲子秀****************
    bottomView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 100*PERCEN)];
    bottomView.backgroundColor = [[UIColor lightGrayColor]colorWithAlphaComponent:.35];
    bottomView.bottom = self.bottom;
    [self addSubview:bottomView];
    
    float itemWidth = bottomView.width / 3.;
    for (int i=0; i<3; i++) {
        UILabel *titleLb = [[UILabel alloc]initWithFrame:CGRectMake(itemWidth*i, (WIDTH_5S ? 6*PERCEN : 10*PERCEN), itemWidth, 20)];
        titleLb.backgroundColor = [UIColor clearColor];
        titleLb.textAlignment = NSTextAlignmentCenter;
        titleLb.textColor = UIColorFromHex(0xffffff);
        titleLb.font = [UIFont systemFontOfSize:13.f];
        titleLb.tag = 10+i;
        [bottomView addSubview:titleLb];
        
        UILabel *titleDetailLb = [[UILabel alloc]initWithFrame:CGRectMake(itemWidth*i, titleLb.bottom-2*PERCEN, itemWidth, 20)];
        titleDetailLb.bottom = bottomView.height - 10*PERCEN;
        titleDetailLb.backgroundColor = [UIColor clearColor];
        titleDetailLb.textAlignment = NSTextAlignmentCenter;
        titleDetailLb.textColor = UIColorFromHex(0xffffff);
        titleDetailLb.font = [UIFont systemFontOfSize:14.f];
        titleDetailLb.tag = 13+i;
        [bottomView addSubview:titleDetailLb];
        
        UIView *lineView = [[UIView alloc]initWithFrame:CGRectMake(itemWidth-1 + itemWidth*i, 15*PERCEN, 1, 70*PERCEN)];
        lineView.backgroundColor = UIColorFromHex(0xcccccc);
        lineView.tag = 16+i;
        [bottomView addSubview:lineView];
        
        UIButton *bt = [UIButton buttonWithType:UIButtonTypeCustom];
        bt.frame = CGRectMake(itemWidth*i, 0, itemWidth, bottomView.height);
        bt.backgroundColor = [UIColor clearColor];
        bt.tag = 19+i;
        [bottomView addSubview:bt];
        
        if (i == 0) {
            titleLb.text = @"0";
            titleDetailLb.text = @"关注";
            _gzLb = titleLb;
            [bt addTarget:self action:@selector(onSelectGZAction) forControlEvents:UIControlEventTouchUpInside];
        }
        else if (i == 1) {
            titleLb.text = @"0";
            titleDetailLb.text = @"粉丝";
            _fanceLb = titleLb;
            [bt addTarget:self action:@selector(onSelectFanceAction) forControlEvents:UIControlEventTouchUpInside];
        }
        else if (i == 2) {
            titleLb.text = @"0";
            titleDetailLb.text = @"亲子秀";
            _qzxLb = titleLb;
            lineView.hidden = YES;
            [bt addTarget:self action:@selector(onSelectQZXAction) forControlEvents:UIControlEventTouchUpInside];
        }
    }
    
}

//头像点击
- (void)onSelectPicImgAction
{
    if (_delegate && [_delegate respondsToSelector:@selector(onSelectItemWithHeadImgForView:)]) {
        [_delegate onSelectItemWithHeadImgForView:self];
    }
}

//描述
- (void)onSelectUserDescribeAction
{
    if (_delegate && [_delegate respondsToSelector:@selector(onSelectItemWithDescribe:withView:)]) {
        [_delegate onSelectItemWithDescribe:@"10" withView:self];
    }
}

//关注
- (void)onSelectGZAction
{
    if (_delegate && [_delegate respondsToSelector:@selector(onSelectItemWithAttention:withView:)]) {
        if (_userDic) {
            NSString *fan = [NSString stringWithFormat:@"%@",[_userDic objectForKey:@"UserID"]];
            [_delegate onSelectItemWithAttention:fan withView:self];
        } else {
            NSString *fan = [NSString stringWithFormat:@"%@",[UserManager standerDefult].userModel.UserID];
            [_delegate onSelectItemWithAttention:fan withView:self];
        }
    }
}

//粉丝
- (void)onSelectFanceAction
{
    if (_delegate && [_delegate respondsToSelector:@selector(onSelectItemWithFans:withView:)]) {
        if (_userDic) {
            NSString *fan = [NSString stringWithFormat:@"%@",[_userDic objectForKey:@"UserID"]];
            [_delegate onSelectItemWithFans:fan withView:self];
        } else {
            NSString *fan = [NSString stringWithFormat:@"%@",[UserManager standerDefult].userModel.UserID];
           [_delegate onSelectItemWithFans:fan withView:self];
        }
    }
}

//亲子秀
- (void)onSelectQZXAction
{
    if (_delegate && [_delegate respondsToSelector:@selector(onSelectItemWithParentChildShow:withView:)]) {
        if (_userDic) {
            NSString *fan = [NSString stringWithFormat:@"%@",[_userDic objectForKey:@"UserID"]];
            [_delegate onSelectItemWithParentChildShow:fan withView:self];
        } else {
            NSString *fan = [NSString stringWithFormat:@"%@",[UserManager standerDefult].userModel.UserID];
            [_delegate onSelectItemWithParentChildShow:fan withView:self];
        }
    }
}

- (void)setDescribeString:(NSString *)describeString
{
    _describeString = describeString;
    [self setNeedsLayout];
}

- (void)setOrientation:(UIDeviceOrientation)orientation
{
    _orientation = orientation;
    [self setNeedsLayout];
}

- (void)setDataDic:(NSDictionary *)dataDic
{
    _dataDic = dataDic;
    [self setNeedsLayout];
}

- (void)setUserDic:(NSDictionary *)userDic
{
    _userDic = userDic;
    [self setNeedsLayout];
}

- (void)setUpdataUI
{
    [self setNeedsLayout];
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    UserModel *model = [UserManager standerDefult].userModel;
    if (model) {
        if (_userDic) {
            //他人
            NSString *face = [_userDic objectForKey:@"FaceImg"];
            if (![face isKindOfClass:[NSNull class]]) {
                if ([face hasPrefix:@"/upload/"]) {
                    [_userImgView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@/%@",MUSIC_PLAY_Url,face]] placeholderImage:[UIImage imageNamed:@"touxiang_attention"]];
                } else {
                    [_userImgView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",model.FaceImg]] placeholderImage:[UIImage imageNamed:@"touxiang_attention"]];
                }
                _userImgView.layer.cornerRadius = _userImgView.height / 2.;
                _userImgView.layer.masksToBounds = YES;
            }else {
                _userImgView.image = [UIImage imageNamed:@"touxiang_attention"];
            }

            NSString *NickName = [_userDic objectForKey:@"NickName"];
            if (![NickName isKindOfClass:[NSNull class]]) {
                _nickNameLb.text = NickName;
            } else {
                _nickNameLb.text = @"米宝兔";
            }
            if ([_userDic.allKeys containsObject:@"MBTID"]) {
                _userIDLb.text = [NSString stringWithFormat:@"ID:%@",[_userDic objectForKey:@"MBTID"]];
            }
            
            NSString *Signature = [_userDic objectForKey:@"Signature"];
            if (![Signature isKindOfClass:[NSNull class]]) {
                _userDetailLb.text = Signature;
            } else {
                 _userDetailLb.text = @"宝宝爱吃爱喝爱睡觉，不服你来比一比!哼哼。";
            }

        } else {
            //自己
            if ([model.FaceImg hasPrefix:@"/upload/"]) {
                [_userImgView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@/%@",MUSIC_PLAY_Url,model.FaceImg]] placeholderImage:[UIImage imageNamed:@"touxiang_attention"]];
            } else {
                [_userImgView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",model.FaceImg]] placeholderImage:[UIImage imageNamed:@"touxiang_attention"]];
            }
            if (model.FaceImg.length > 0) {
                _userImgView.layer.cornerRadius = _userImgView.height / 2.;
                _userImgView.layer.masksToBounds = YES;
            }
            _nickNameLb.text = (model.NickName.length > 0 ? model.NickName:@"米宝兔");
            _userIDLb.text = [NSString stringWithFormat:@"ID:%@",model.MBTID];
            _userDetailLb.text = (model.Signature.length == 0 ? @"宝宝爱吃爱喝爱睡觉，不服你来比一比!哼哼。" : model.Signature);
        }
        
        
        if (_dataDic) {
            _gzLb.text = [NSString stringWithFormat:@"%@",[_dataDic objectForKey:@"AttentionNums"]];
            if ([_gzLb.text isEqualToString:@"<null>"]) {
                _gzLb.text = @"0";
            }
            _fanceLb.text = [NSString stringWithFormat:@"%@",[_dataDic objectForKey:@"FansNums"]];
            if ([_fanceLb.text isEqualToString:@"<null>"]) {
                _fanceLb.text = @"0";
            }
            _qzxLb.text = [NSString stringWithFormat:@"%@",[_dataDic objectForKey:@"ShowNums"]];
            if ([_qzxLb.text isEqualToString:@"<null>"]) {
                _qzxLb.text = @"0";
            }
        }
    }
    
    //frame
    bgImgView.frame = CGRectMake(0, 0, SCREEN_WIDTH, self.height);
    _userImgView.frame = CGRectMake((SCREEN_WIDTH-140*PERCEN)/2., 168*PERCEN, 140*PERCEN, 140*PERCEN);
    useImgbt.frame = _userImgView.frame;
    _nickNameLb.frame = CGRectMake(0, _userImgView.bottom+10*PERCEN, SCREEN_WIDTH, 20);
    _userIDLb.frame = CGRectMake(0, _nickNameLb.bottom-2, SCREEN_WIDTH, 20);
    _userDetailLb.frame = CGRectMake(0, (WIDTH_5S ? _userIDLb.bottom-5*PERCEN : _userIDLb.bottom+3*PERCEN), SCREEN_WIDTH, 20);
    useDetailbt.frame = _userDetailLb.frame;
    bottomView.frame = CGRectMake(0, 0, SCREEN_WIDTH, 100*PERCEN);
    bottomView.bottom = self.bottom;
    float itemWidth = bottomView.width / 3.;
    for (int i=0; i<3; i++) {
        UILabel *titleLb = (UILabel *)[self viewWithTag:10+i];
        UILabel *titleDetailLb = (UILabel *)[self viewWithTag:13+i];
        UIView *lineView = (UIView *)[self viewWithTag:16+i];
        UIButton *bt = (UIButton *)[self viewWithTag:19+i];
        
        titleLb.frame = CGRectMake(itemWidth*i, (WIDTH_5S ? 6*PERCEN : 10*PERCEN), itemWidth, 20);
        titleDetailLb.frame = CGRectMake(itemWidth*i, titleLb.bottom-2*PERCEN, itemWidth, 20);
        lineView.frame = CGRectMake(itemWidth-1 + itemWidth*i, 15*PERCEN, 1, 70*PERCEN);
        bt.frame = CGRectMake(itemWidth*i, 0, itemWidth, bottomView.height);
    }
}

@end
